﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BaseHelper.Base
{
    public class ConfigHelper
    {
        private static SortedList<string, Control> _slControl = new SortedList<string, Control>();
        private SortedList<string, string> _slConfig = new SortedList<string, string>();
        private static string _filePath;
        public static string URLFile;

        public int GetInt(string key, int def = 0)
        {
            try
            {
                return Convert.ToInt32(Get(key, def.ToString()));
            }
            catch { }
            return def;
        }

        public bool GetBool(string key, bool def = false)
        { 
            return Get(key,def.ToString()).Equals("True");
        }

        public string Get(string key, string def = "")
        {
            if (_slConfig.ContainsKey(key))
                return _slConfig[key];

            return def;
        }

        public void Get(Control obj,string def="")
        {
            string val = Get(obj.Name, def);

            try
            {
                if (obj is TextBox || (obj is ComboBox && ((ComboBox)obj).DropDownStyle == ComboBoxStyle.Simple))
                {
                    obj.Text = val;
                }
                else if (obj is ComboBox)
                {
                    ((ComboBox)obj).SelectedIndex = Convert.ToInt32(val);
                }
                else if (obj is CheckBox)
                {
                    CheckBox chk = (CheckBox)obj;
                    chk.Checked = val.Equals("True");
                }
                else if (obj is RadioButton)
                {
                    RadioButton rd = (RadioButton)obj;
                    rd.Checked = val.Equals("True");
                }
                else if (obj is NumericUpDown)
                {
                    NumericUpDown nd = (NumericUpDown)obj;
                    nd.Value = Convert.ToInt32(def);
                }
            }
            catch { }
            _slControl[obj.Name] = obj;
        }

        public void Set(string key, bool value)
        {
            Set(key, value.ToString());
        }

        public void Set(string key, int value)
        {
            Set(key,value.ToString());
        }
        public void Set(string key, string value)
        {
            _slConfig[key] = value;
        }

        public bool Load(string filePath)
        {
            _filePath = filePath;
            object obj = ObjectToFileHelper.Load(filePath);
            if (obj != null)
            {
                _slConfig = (SortedList<string, string>)obj; 
                return true;
            }
            return false;

        }

        public void Save()
        {
            foreach (Control obj in _slControl.Values)
            {
                if (obj is TextBox || (obj is ComboBox && ((ComboBox)obj).DropDownStyle == ComboBoxStyle.Simple))
                {
                    _slConfig[obj.Name] = obj.Text;

                }
                else if (obj is ComboBox)
                {
                    _slConfig[obj.Name] = ((ComboBox)obj).SelectedIndex.ToString();
                }
                else if (obj is CheckBox)
                {
                    CheckBox chk = (CheckBox)obj;
                    _slConfig[obj.Name] = chk.Checked.ToString();
                }
                else if (obj is RadioButton)
                {
                    RadioButton rd = (RadioButton)obj;
                    _slConfig[obj.Name] = rd.Checked.ToString();
                }

            }

            ObjectToFileHelper.Save(_filePath, _slConfig);
        }
    }
}
